package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询spu
     * @param key 查询关键字
     * @param saleable 上架商品或下架商品
     * @param page 当前页
     * @param rows 每页显示数量
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false)Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows
    ){
        return ResponseEntity.ok(goodsService.querySpuByPage(key,saleable,page,rows));
    }

    /**
     * 新增商品
     * @param spuDTO 页面提交商品信息Spu、SpuDetail、Sku
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改商品上下架
     * @param id 商品spu的id
     * @param saleable true：上架；false：下架
     * @return
     */
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id")Long id,@RequestParam("saleable")Boolean saleable){
        goodsService.updateSpuSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spuId查询spuDetail
     * @param id
     * @return
     */
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id){
        return ResponseEntity.ok(goodsService.querySpuDetailById(id));
    }

    /**
     * 根据spuID查询sku
     * @param id spuID
     * @return sku的集合
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id){
        return ResponseEntity.ok(goodsService.querySkuBySpuId(id));
    }

    /**
     * 修改商品
     * @param spuDTO
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 通过spuId查询spu
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.querySpuById(id));
    }


    /**
     * 根据ids 查询 sku的集合
     * @param ids
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(this.goodsService.querySkuByIds(ids));
    }


    /**
     * 扣减库存，基于sku的map集合，map的key存放sku的Id，map的value对应的是当前sku应该扣减的数量
     * @param skuMap
     * @return
     */
    @PutMapping("sku/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long,Integer> skuMap){

        this.goodsService.minusStock(skuMap);
        return ResponseEntity.ok().build();
    }


    /**
     * 加库存，基于sku的map集合，map的key存放sku的Id，map的value对应的是当前sku应该增加的数量
     * @param skuMap
     * @return
     */
    @PutMapping("sku/plus")
    public ResponseEntity<Void> plusStock(@RequestBody Map<Long,Integer> skuMap){

        this.goodsService.plusStock(skuMap);
        return ResponseEntity.ok().build();
    }
}
